package Colecciones;

import Modelo.AtributoTipo;
import Modelo.AtributoValor;
import Modelo.EstructuraFila;
import Modelo.Fila;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Vector;
import org.apache.commons.lang.StringUtils;

public class Utils
{
  private static boolean DEBUG = false;

  static void write(String string)
  {
    if (DEBUG)
      System.out.println(string);
  }

  static String quitaTildes(String cadena)
  {
    return StringUtils.replaceChars(cadena, "áéíóúNÁÉÍÓÚÑ", "aeiounAEIOUN");
  }

  static boolean notEmpty(String value)
  {
    if (value == null) {
      return false;
    }

    return (value.trim().length() != 0);
  }

  static String quitaEspacios(String cadena)
  {
    return StringUtils.deleteWhitespace(cadena);
  }

  static String getUpdateString(Object obj, EstructuraFila ef, String tableName)
    throws MetadataException, IllegalAccessException, InvocationTargetException
  {
    Class clase = obj.getClass();
    Vector v_campos_tabla = new Vector();
    Vector v_values = new Vector();
    recorrido_campos(ef, clase, obj, v_campos_tabla, v_values);

    String strUpdate = "UPDATE " + tableName + " SET ";
    for (int i = 0; i < v_campos_tabla.size(); ++i) {
      strUpdate = strUpdate + " ";
      strUpdate = strUpdate + v_campos_tabla.get(i);
      strUpdate = strUpdate + " = ";
      strUpdate = strUpdate + v_values.get(i);
    }
    strUpdate = strUpdate + " ";

    return strUpdate;
  }

  public static String getInsertString(Object obj, EstructuraFila ef, String tableName)
    throws MetadataException, IllegalAccessException, InvocationTargetException
  {
    Class clase = obj.getClass();
    Vector v_campos_tabla = new Vector();
    Vector v_values = new Vector();
    recorrido_campos(ef, clase, obj, v_campos_tabla, v_values);

    String strInsert = "INSERT INTO " + tableName + " (";
    strInsert = strInsert + StringUtils.join(v_campos_tabla, ",");
    strInsert = strInsert + ") VALUES ( ";
    strInsert = strInsert + StringUtils.join(v_values, ",");
    strInsert = strInsert + ")";
    return strInsert;
  }

  public static void recorrido_campos(EstructuraFila ef, Class clase, Object obj, Vector v_campos_tabla, Vector v_values)
    throws MetadataException, IllegalAccessException, InvocationTargetException
  {
    if (obj == null)
      throw new IllegalArgumentException("la metadata no puede ser nula");

    for (int i = 0; i < ef.size(); ++i) {
      AtributoTipo at;
      Method metodo;
      try {
        at = ef.getAtributoTipo(i);
        metodo = clase.getMethod(at.getMetodo(), new Class[0]);
      } catch (Exception e) {
        MetadataException me = new MetadataException("Hay una referencia a un atributo inexistente", e);
        me.printStackTrace();
        throw me;
      }
      AtributoValor resultado = (AtributoValor)metodo.invoke(obj, new Object[0]);

      if ((resultado.getValor() != null) && (resultado.getValor().length() > 0)) {
        String valorCampo = "";
        if ("S".equalsIgnoreCase(at.getTipo()))
          valorCampo = valorCampo + " '" + resultado.getValor() + "' ";

        if ("N".equalsIgnoreCase(at.getTipo()))
          valorCampo = valorCampo + " " + resultado.getValor() + " ";

        if ("D".equalsIgnoreCase(at.getTipo()))
          valorCampo = valorCampo + " TO_DATE('" + resultado.getValor() + "','MM/DD/YYYY')" + " ";

        v_campos_tabla.add(resultado.getAtributo());
        v_values.add(valorCampo);
      }
      else if (at.getObligatoriedad()) {
        throw new IllegalArgumentException("El método " + at.getMetodo() + "() es Obligatorio que tenga un valor asociado ");
      }
    }
  }

  static String getValor(Fila fila, String llave)
  {
    try
    {
      return fila.getValor(llave); } catch (Exception ex) {
    }
    return null;
  }

  public static void main(String[] args)
  {
    String testStr = "    Alex Roldán HD,   áéíóú     ";
    System.out.println("Sin tildes: " + quitaTildes(testStr));
    System.out.println("Sin espacios: " + quitaEspacios(testStr));
    System.out.println("Sin tildes y espacios: " + quitaEspacios(quitaTildes(testStr)));
  }

  public static Vector dividirString(String sCadena)
  {
    Vector vector = new Vector();

    if ((sCadena != null) && (!(sCadena.equals("")))) {
      if (sCadena.contains(","))
      {
        int iPosInicial = 0;
        int iPosComa = 0;
        while (sCadena.contains(","))
        {
          iPosComa = sCadena.indexOf(",");
          String strValor = sCadena.substring(iPosInicial, iPosComa);
          if ((strValor != null) && (!(strValor.equals(""))))
            vector.add(strValor.trim());
          sCadena = sCadena.substring(iPosComa + 1, sCadena.length());
        }
      }
      if ((sCadena != null) && (!(sCadena.equals(""))))
        vector.add(sCadena.trim());
    }

    return vector;
  }
}